package com.jungeun.pickpics;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;

public class PickPicsProvider extends ContentProvider {

	static final String DATABASE_NAME = "pickpics.db";
	static final String DATABASE_TABLE = "pickpics";
	static final int DATABASE_VERSION = 1;

	static final String KEY_ID = "_id";
	static final String KEY_WIDTH = "width";
	static final String KEY_HEIGHT = "height";
	static final String KEY_BITMAP = "bitmap";

	static final int ALL_ROWS = 0;
	static final int SINGLE_ROW = 1;

	private static final UriMatcher uriMatcher;

	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher
				.addURI("com.jungeun.pickpics.provider", "pickpics", ALL_ROWS);
		uriMatcher.addURI("com.jungeun.pickpics.provider", "pickpics/#",
				SINGLE_ROW);
	}

	private SQLiteDatabase mDB;

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int count;

		switch (uriMatcher.match(uri)) {
		case ALL_ROWS:
			count = mDB.delete(DATABASE_TABLE, selection, selectionArgs);
			break;

		case SINGLE_ROW:
			String noteId = uri.getPathSegments().get(1);
			count = mDB.delete(DATABASE_TABLE, KEY_ID
					+ "="
					+ noteId
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
							+ ')' : ""), selectionArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		Context context = getContext();

		PickpicsProviderDatabaseHelper providerDatabaseHelper;

		providerDatabaseHelper = new PickpicsProviderDatabaseHelper(context,
				DATABASE_NAME, null, DATABASE_VERSION);

		mDB = providerDatabaseHelper.getWritableDatabase();

		return (mDB == null) ? false : true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	private static class PickpicsProviderDatabaseHelper extends
			SQLiteOpenHelper {

		// Definition of Database Create
		private static final String DATABASE_CREATE = /*
													 * "CREATE TABLE " +
													 * DATABASE_TABLE + " (" +
													 * KEY_ID +
													 * " INTEGER PRIMARY KEY," +
													 * FIELD_PHOTO_BLOB +
													 * " TEXT," +
													 * FIELD_WIDGET_SIZE +
													 * " INTEGER" + ");
													 */"";

		public PickpicsProviderDatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
	}

}
